package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.selectList(
                new QueryWrapper<ShoppingCart>()
                        .eq("user_id", BaseContext.getCurrentId())
                        .orderByDesc("create_time")
        );
    }

    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(new QueryWrapper<ShoppingCart>()
                .eq("user_id", BaseContext.getCurrentId())
                .eq(shoppingCartDTO.getSetmealId() != null, "setmeal_id", shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishId() != null, "dish_id", shoppingCartDTO.getDishId()));

        if (shoppingCart.getNumber() > 1) {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateById(shoppingCart);
        } else {
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }
    }

    @Override
    public void insert(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(new QueryWrapper<ShoppingCart>()
                .eq("user_id", BaseContext.getCurrentId())
                .eq(shoppingCartDTO.getSetmealId() != null, "setmeal_id", shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishId() != null, "dish_id", shoppingCartDTO.getDishId()));

        if (shoppingCart != null) {
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateById(shoppingCart);
        } else {
            shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            if (shoppingCartDTO.getSetmealId() != null) {
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            } else {
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public void clean() {
        shoppingCartMapper.delete(
                new QueryWrapper<ShoppingCart>().eq("user_id", BaseContext.getCurrentId())
        );
    }
}
